package com.centling.smartSealForPhone.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import com.centling.smartSealForPhone.fragment.StampActivity;

import com.wsdk.view.ProcessDialog;
import com.wsdk.activity.WsdkActivity;

@SuppressLint("Registered")
public class BaseActivity
        extends WsdkActivity {
    public static final String DOWNLOAD_FIRM_PATH = Environment.getExternalStorageDirectory()
            .getPath() + "/com.centling.seal.phone/download/firmware/";

    protected Activity mActivity;
    protected ProcessDialog mProcessDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setCanConnectDevice(this instanceof MainTabsActivity
                || this instanceof BluetoothListActivity
                || this instanceof StampActivity
                || this instanceof StampTimeLimitActivity
                || this instanceof SealChangeTipsActivity
                || this instanceof BossModeActivity
                || this instanceof BossModeDetailActivity
                || this instanceof BugReportActivity);
        setNoNeedDealWithResetKey(this instanceof BluetoothListActivity);
        super.onCreate(savedInstanceState);

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        mActivity = this;
        mProcessDialog = new ProcessDialog(this);
        AtyManager.INSTANCE.addActivity(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        AtyManager.INSTANCE.removeActivity(this);
    }

    public void changeFragment(Fragment fragment, boolean first, int id) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (!first) {
            transaction.addToBackStack(null);
        }
        transaction.replace(id, fragment);
        transaction.commitAllowingStateLoss();
    }

    @Override
    public void onBackPressed() {
        FragmentManager manager = getSupportFragmentManager();
        if (manager != null && manager.getBackStackEntryCount() > 0) {
            manager.popBackStackImmediate();
        } else {
            finish();
        }
    }
}
